﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.Currency.TextUtils;
using CatUtils.Utils.SpecialUtils.Html;
using CatUtilsDll.Utils.Currency.WebUtils;
using Newtonsoft.Json;
using RabbitDownload.Helper.Currency;

using RabbitDownload.WebSite.WebCurrency;
using System;
using System.Collections.Generic;

namespace RabbitDownload.WebSite.Pixiv.PixivDay
{
    internal class PixivDayEnhanceTool
    {
        /// <summary>
        /// 批量初始化API
        /// </summary>
        /// <param name="Day"></param>
        /// <param name="API"></param>
        /// <returns></returns>
        public List<string> InitApi(String Day, String API)
        {
            Console.WriteLine(Day);
            List<string> ApiList = new List<string>();
            API = string.Format(API, Day, "{0}");
            for (int i = 1; i <= 5; i++)
            {
                ApiList.Add(string.Format(API, i));
                Console.WriteLine(string.Format(API, i));
            }
            return ApiList;
        }

        /// <summary>
        /// 导入API列表， 开始扫描
        /// </summary>
        /// <param name="ApiList"></param>
        /// <returns></returns>
        public void ScannerArtID(List<String> ApiList)
        {
            List<string> ALLArticleId = new List<string>();
            int ComplentCount = 0;
            foreach (string Line in ApiList)
            {
                new PrintLog().PercentLog(ApiList.Count, ++ComplentCount);
                ALLArticleId.AddRange(AnalysisArticleId(Line));
            }
            Console.WriteLine("保存在list文件夹下，P站画师ID.txt");
            ALLArticleId = ListHelper.ListRepeat(ALLArticleId);
            SaveHelper.SaveList(ALLArticleId, "P站画师ID列表", "P站画师ID");
        }

        private List<string> AnalysisArticleId(string API)
        {
            string Shtml = WebHelper.HttpPcGet(API);
            Shtml = new PixivHelper().StandartJson(Shtml);
            List<string> Templist = new List<string>();
            try
            {
                PixivDayJson.Root AllJson = JsonConvert.DeserializeObject<PixivDayJson.Root>(Shtml);

                foreach (PixivDayJson.ContentsItem SingleItem in AllJson.contents)
                {
                    Templist.Add(SingleItem.user_id.ToString());
                }
            }
            catch (Exception ex)
            {
                PrintLog.Log(ex);
            }

            return Templist;
        }

        /// <summary>
        /// 获取某个作者作品数
        /// </summary>
        /// <param name="ArticleId"></param>
        /// <returns></returns>
        public int GetArticleCount(string ArticleId)
        {
            string API = string.Format("https://www.pixiv.net/member.php?id={0}", ArticleId);
            string Shtml = WebHelper.HttpPcGet(API);

            string GetScore = HtmlHelper.GetFirstText(Shtml, "//div[@class='count-container']/div[@class='count']/a");

            return TextHelper.StringToInt(GetScore);
        }

        /// <summary>
        /// 导入作者列表文件，清理作品低于某个数的画师
        /// </summary>
        /// <param name="FilePath"></param>
        /// <param name="TextCount"></param>
        public void ClearArticleId(string FilePath, string TextCount)
        {
            int Count = TextHelper.StringToInt(TextCount);
            List<string> Quafilied = new List<string>();
            List<string> ArticleList = FileHelper.ReadAllLine(FilePath);
            int Complted = 0;

            foreach (string SingleId in ArticleList)
            {
                int ArticleCount = GetArticleCount(SingleId);
                Console.WriteLine("作品数：" + ArticleCount);
                new PrintLog().PercentLog(ArticleList.Count, ++Complted);
                if (ArticleCount > Count)
                {
                    Console.WriteLine("数量{0}大于{1}，已收集。", ArticleCount, Count);
                    Quafilied.Add(SingleId);
                }
            }
            string SaveFilePath = FileNameHelper.CreateFileNameAddTip(FilePath, string.Format("作品数大于{0}", Count));
            FileHelper.WriteList(SaveFilePath, Quafilied);
            Console.WriteLine("保存在：" + SaveFilePath);
        }
    }
}